//
//  NetworkTool.swift
//  LRWeibo
//
//  Created by admin on 15/9/23.
//  Copyright © 2015年 懒人. All rights reserved.
//

import Foundation
import AFNetworking

/// 网络请求工具类
class NetworkTool: AFHTTPSessionManager{
    
    /// 请求枚举类型
    enum requestMethod: String
    {
        case GET = "GET"
        case POST = "POST"
    }
    
    /// 网络请求单利
    static let shareNetTool: NetworkTool = {
        let instance = NetworkTool(baseURL: nil)
        instance.responseSerializer.acceptableContentTypes.insert("text/plain")

        return instance
        }()
    
    /// GET/POST请求
    func request(method: requestMethod, URLString: String!, prams: AnyObject!, finshBlock:(result: AnyObject?, error: NSError?)->())
    {

        if method == requestMethod.GET
        {
            // get请求
            GET(URLString, parameters: prams, success: { (task: NSURLSessionDataTask!, result: AnyObject!) -> Void in
                finshBlock(result: result, error: nil)
                }) { (task: NSURLSessionDataTask!, error: NSError!) -> Void in
                    finshBlock(result: nil, error: error)
            }
        }else{
            // post请求
            POST(URLString, parameters: prams, success: { (task: NSURLSessionDataTask!, result: AnyObject!) -> Void in
                finshBlock(result: result, error: nil)
                }) { (task: NSURLSessionDataTask!, error: NSError!) -> Void in
                    finshBlock(result: nil, error: error)
            }
        }
    }

}